home *** CD-ROM | disk | FTP | other *** search
/ Aminet 48 / Aminet 48 (2002)(GTI - Schatztruhe)[!][Apr 2002].iso / Aminet / dev / src / expat-src.lha / expat-1.95.2 / xmlwf / unixfilemap.c < prev    next >
Encoding:
C/C++ Source or Header  |  2001-07-28  |  1.1 KB  |  59 lines

  1. /*
  2. Copyright (c) 1998, 1999 Thai Open Source Software Center Ltd
  3. See the file COPYING for copying permission.
  4. */
  5.  
  6. #include <sys/types.h>
  7. #include <sys/mman.h>
  8. #include <sys/stat.h>
  9. #include <fcntl.h>
  10. #include <errno.h>
  11. #include <string.h>
  12. #include <stdio.h>
  13. #include <unistd.h>
  14.  
  15. #ifndef MAP_FILE
  16. #define MAP_FILE 0
  17. #endif
  18.  
  19. #include "filemap.h"
  20.  
  21. int filemap(const char *name,
  22.         void (*processor)(const void *, size_t, const char *, void *arg),
  23.         void *arg)
  24. {
  25.   int fd;
  26.   size_t nbytes;
  27.   struct stat sb;
  28.   void *p;
  29.  
  30.   fd = open(name, O_RDONLY);
  31.   if (fd < 0) {
  32.     perror(name);
  33.     return 0;
  34.   }
  35.   if (fstat(fd, &sb) < 0) {
  36.     perror(name);
  37.     close(fd);
  38.     return 0;
  39.   }
  40.   if (!S_ISREG(sb.st_mode)) {
  41.     close(fd);
  42.     fprintf(stderr, "%s: not a regular file\n", name);
  43.     return 0;
  44.   }
  45.   
  46.   nbytes = sb.st_size;
  47.   p = (void *)mmap((caddr_t)0, (size_t)nbytes, PROT_READ,
  48.            MAP_FILE|MAP_PRIVATE, fd, (off_t)0);
  49.   if (p == (void *)-1) {
  50.     perror(name);
  51.     close(fd);
  52.     return 0;
  53.   }
  54.   processor(p, nbytes, name, arg);
  55.   munmap((caddr_t)p, nbytes);
  56.   close(fd);
  57.   return 1;
  58. }
  59.